package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Spring Boot 应用启动类
 * <p>
 * Created by bysocket on 16/4/26.
 */
// Spring Boot 应用的标识
//@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan("com.example")
public class MailApplication implements CommandLineRunner {

    @Autowired
    private MailService mailService;

    public static void main(String[] args) {
        SpringApplication.run(MailApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        MailMessage mailMessage = getTempletesMailMessage();

        mailService.send(mailMessage);
      //  SimpleMailMessage simpleMail = getSimpleMail();
//
//        mailService.send(simpleMail);
    }

    private SimpleMailMessage getSimpleMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("919055887@qq.com");
        message.setTo("1360652124@qq.com");
        message.setSubject("主题：简单邮件");
        message.setText("测试邮件内容");
        return message;
    }

    private MailMessage getTempletesMailMessage() {
        MailMessage mailMessage = new MailMessage();
        mailMessage.setTemplateId("emailTemplate");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("username", "张三");
        mailMessage.setParamMap(paramMap);
        mailMessage.setFrom("919055887@qq.com");
        mailMessage.setToList(Arrays.asList("1360652124@qq.com"));
        mailMessage.setSubject("Hello Spring Boot Java Mail");
        return mailMessage;
    }


//    public void sendSimpleMail() throws Exception {
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom("919055887@qq.com");
//        message.setTo("1360652124@qq.com");
//        message.setSubject("主题：简单邮件");
//        message.setText("测试邮件内容");
//
//        mailService.send(message);
//    }
}

